#include "WXTProtocol.h"

WXTProtocol::WXTProtocol(QObject *parent) :
    BaseProtocol(parent)
{
    this->m_protocolType = ProtocolType::WXTProtocol;
}

QByteArray WXTProtocol::generateData(Package package)
{
    QByteArray data;
    data.append(package.body());

    return data;

}

QList<Package> WXTProtocol::parseData(QByteArray data)
{
    QList<Package> packageList;
    Package package;
    bool ok;
      QJson::Parser parser;
     // qDebug()<<"WXTProtocol::parseData";


   //   qDebug()<<"parseData_data:"<<data;
      QVariant resultQvar=parser.parse(data, &ok);
      QVariantMap result = resultQvar.toMap();

      if(ok){
          bool sucess = result["success"].toBool();
          if(!sucess)
          {
              qDebug()<<"sucess is not true";
              return packageList;
          }



          QString cmd =  result["cmd"].toString();
          qDebug()<<"WXT cmd:"<<cmd;
          if(cmd == "registry/user/login")
          {
                QVariantMap _data = result["data"].toMap();
                QString accessToken = _data["access_token"].toString();
                qDebug()<<"accessToken:"<<accessToken;
                package.setFunction(LoginLewin);
                package.setBody(accessToken.toLatin1());
                packageList.append(package);
          }else if(cmd=="api/v1/se/key/issue"){
               QVariant list=result.value("data");

                //QVariantList list = qvariant_cast<QVariantList>(variant);
               foreach (QVariant val, list.toList()) {

                   QVariantMap map=val.toMap();
                    qDebug()<<"map :"<<map["keyEnc"];
                    package.setFunction(IssueKey);
                    package.setBody(map["keyEnc"].toString().toLatin1());
                    packageList.append(package);
               }
          }else if(cmd=="messageAuth"){
                   //qDebug()<<"messageAuth :"<<data;
                    package.setFunction(MessageAuthentication);
                    package.setBody(data);
                    packageList.append(package);

            /*
              QString data_str= result["data"].toString();
              QByteArray datta_str_bytearray=data_str.toUtf8();
              qDebug()<<"data_str:"<<data_str;


              QStringList list1= parser.parse(datta_str_bytearray,&ok).toStringList();
              foreach (QString val, list1) {
                  QByteArray val_array=val.toUtf8();
                  qDebug()<<"val_array:"<<val_array;
                  QVariantMap resultVal = parser.parse(val_array, &ok).toMap();
                   qDebug()<<"resultVal___keyEnc:"<<resultVal;
              }

              QVariantMap _data = result["data"].toMap();
              QString keyEnc = _data["keyEnc"].toString();
              qDebug()<<"keyEnc:"<<keyEnc;
              package.setFunction(IssueKey);
              package.setBody(keyEnc.toLatin1());
              packageList.append(package);*/
          }
       /* if(message == QString::fromLocal8Bit("请输入正确token"))
         {
             qDebug()<<"invalid token";
             package.setFunction(InvalidToken);

             package.setBody("0");

              packageList.append(package);
         }

      else   if((message == QString::fromLocal8Bit("token已过时，请重新获取。"))||(code=="507"))
         {
             qDebug()<<"invalid token";
             package.setFunction(InvalidToken);

             package.setBody("0");

              packageList.append(package);
         }
       else  if(message == QString::fromLocal8Bit("请先获取token，然后登录。"))
         {
             qDebug()<<"invalid token";
             package.setFunction(InvalidToken);

             package.setBody("0");

              packageList.append(package);
         }
        if(title == QString::fromLocal8Bit("获取公钥信息"))
         //  if(title.toLocal8Bit() == QString::fromLocal8Bit("系统获取公钥信息"))
          //    if(title == "获取公钥信息")
          {
               if((code == "200")&&(message == "success"))
               {
              qDebug()<<"getpublickey sucess";
              package.setFunction(GetPublicKey);
              QVariantMap list = _result["list"].toMap();
              QVariantMap item = list["item"].toMap();
              QString publicKey = item["publicKey"].toString();
              package.setBody(publicKey.toLatin1());

               packageList.append(package);
               }
          }
           else if(title == QString::fromLocal8Bit("第三方系统登录获取token"))
           {
                qDebug()<<"gettoken...";
                if((code == "200")&&(message == "success"))
                {
                    qDebug()<<"gettoken sucess";
                    package.setFunction(GeToken);
                    QVariantMap list = _result["list"].toMap();
                    QVariantMap item = list["item"].toMap();
                    QString token = item["token"].toString();
                    package.setBody(token.toLatin1());

                    packageList.append(package);
                }
                else
                {
                    qDebug()<<"gettoken fail";
                }
            }
        else if(title == QString::fromLocal8Bit("获取BMS设备点位信息"))
        {
                qDebug()<<"getPointInfo...";
                if((code == "200")&&(message == "success"))
                {
                    qDebug()<<"getPointInfo sucess";
                    package.setFunction(GetPointInfo);
                    QVariantMap list = _result["list"].toMap();

                    QVariant item = list["item"];

                    foreach (QVariant m,item.toList()) {

                        QVariantMap details = m.toMap();
                        package.setMapBody(details);
                    }


                    packageList.append(package);
                }
                else
                {
                    qDebug()<<"getPointInfo fail";
                }
        }

          else

              qDebug()<<"getPUblickEY fail!!";*/

      }

    return packageList;
}

